var productVue = new Vue({
	el: "#app",
	data: {
		num:1,
		imgUrlRoot:"https://app.meijiangzi.com/static/",
		imgUrl: "https://app.meijiangzi.com/static/",
		products: [],
		typelist: [],
		isExcute:false,
		isBottom:false,
		pageNum:1,
		banners:[],
		translate:0,
		picWidth:750
	},
	methods: {
		getBanner:function(){
			var t = this;
			wx.showLoading({
				title: "加载中",
				mask: !0
			});
			this.$http.post(baseUrl+"banner/query",{
				type:'jfsc'
			}).then(function(e){
				wx.hideLoading();
				this.banners = e.data.data;
				this.bannersRock(true);
			}).catch(function(e){
				wx.hideLoading();
				wx.showToast({
					title: "网络错误，请检查网络",
					icon: "none",
					duration: 2e3,
					mask: !0
				});
			});
		},
		getType: function() {
			var t = this;
			wx.showLoading({
				title: "加载中",
				mask: !0
			});
			this.$http.get(baseUrl + "productTypeCode/query").then(function(a) {
				wx.hideLoading();
				i = a.data;
				if (!i.success) return wx.hideLoading(), wx.showToast({
					title: i.description,
					icon: "none",
					duration: 2e3,
					mask: !0
				}), !1;
				console.log(i.data), t.typelist = i.data.slice(0, 3);
				t.typelist.forEach(function(e){
					if(e.imgUrl){
						e.imgUrl = e.imgUrl.replace('http://mjz.yitavc.com/static/', '../../images/');
					}
				});
			}).catch(function(t) {
				wx.hideLoading();
				wx.showToast({
					title: "网络错误，请检查网络",
					icon: "none",
					duration: 2e3,
					mask: !0
				});
			});
		},
		to_type:function(t){
			Jump.to(page.product.type,"typeId="+t);
		},
		getMall: function() {
			var t = this;
			if (this.isBottom) return !1;
			wx.showLoading({
				title: "加载中",
				mask: !0
			}), this.$http.post(baseUrl + "wap/product/index", {
				currentPage: this.pageNum,
				pageSize: 10
			}).then(function(a) {
				var s = a.data;
				wx.hideLoading();
				if (!s.success) return wx.hideLoading(), wx.showToast({
					title: s.description,
					icon: "none",
					duration: 2e3,
					mask: !0
				}), !1;
				if (s.data.length > 0 && 1 == t.pageNum)t.products = s.data;
				else if (s.data.length > 0 && t.pageNum > 1) {
					s.data.forEach(function(e) {
						t.products.push(e);
					})
				} else if (0 == s.data.length && t.pageNum > 1) return wx.showToast({
					title: "已经到最底下了",
					icon: "none",
					duration: 1e3,
					mask: !0
				}), t.isBottom = !0, !1;
			}).catch(function(t) {
				wx.hideLoading(), console.log(t), wx.showToast({
					title: "网络错误，请检查网络",
					icon: "none",
					duration: 2e3,
					mask: !0
				});
			});
			t.isExcute = false;
		},
		detail:function(t){
			Jump.to(page.product.detail,"guid="+t.productGuid);
		},
		handleScroll: function() {
			// 防止重复
			if (this.isExcute) {
				return;
			}
			if (this.isBottom) {
				return;
			}
			var flag = srollEvent.isReachBottom();
			if (!flag) {
				return;
			}
			this.isExcute = true;
			this.pageNum++;
			this.getMall();
		},
		toSearch:function(){
			Jump.to(page.search.input,"type=2");
		},
		bannersRock:function(init){
			var t = this;
			if(init){
				t.left = new Array();
				var banners = this.banners;
				banners.forEach(function(e,index){
					if(index==banners.length-1){
						return;
					}
					t.left.push((index+1)*t.picWidth*-1);
				});
				t.right = new Array();
				t.flag = true;
				
			}
			t.inter = setInterval(function(){
				if(t.flag){
					t.translate = t.left.shift();
					t.right.push(t.translate);
					if(t.left.length==0){
						t.flag =false;
					}
				}else{
					var translate = t.right.pop();
					t.translate = translate+t.picWidth;
					t.left.push(translate);
					if(t.right.length==0){
						t.flag = true;
						t.left.reverse();
					}
				}
			},2000)
			
		},
		to_product:function(item){
			Jump.to(page.product.detail,"guid="+item.businessId);
		},
		touchStart: function(ev) {
			ev = ev || event;
			ev.preventDefault();
			//                      console.log(ev.targetTouches);
			//                      console.log(ev.changedTouches);
			if (ev.touches.length == 1) { //tounches类数组，等于1时表示此时有只有一只手指在触摸屏幕
				this.startX = ev.touches[0].clientX; // 记录开始位置
			}
		},
		touchEnd: function(ev, e,index) {
			ev = ev || event;
			ev.preventDefault();
			if (ev.changedTouches.length == 1) {
				var endX = ev.changedTouches[0].clientX;
				this.disX = endX - this.startX;
				if (this.disX < 0) {
					// 左滑
					if(index==this.banners.length-1){
						return;
					}else{
						clearInterval(this.inter)
						this.translate = this.translate-this.picWidth;
						this.bannersRock(false);
					}
				} else if(this.disX >0) {
					// 右滑
					if(index==0){
						return;
					}else{
						clearInterval(this.inter)
						this.translate = this.translate+this.picWidth;
						this.bannersRock(false);
					}
				}else{
					this.to_product(e);
				}
			}
		},
	},
	mounted: function() {
		window.addEventListener("scroll", this.handleScroll, {
			passive: false
		});
	},
	created: function() {
		this.picWidth = window.screen.width*2;
		this.getBanner();
		// this.getType(); 直接页面写死了几个分类
		this.getMall();
	}
});
